﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Business;
using SD.DataAccess;

namespace SD.Web.Iteration1.Program
{
    public partial class EditProgram : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        /// <summary>
        /// Fill the Info of the Program to the form
        /// </summary>
        private void LoadData()
        {
            ProgramView newProgram = new ProgramBUS().GetProgram(Convert.ToInt32(Request.QueryString["programId"].ToString()));
            TextBoxName.Text = newProgram.ProgramName; 
            TextBoxDescription.Text = newProgram.Description;
            TextBoxManagerName.Text = newProgram.FirstName + " " + newProgram.Surname;
            HiddenFieldManagerId.Value = newProgram.ProgramId + "";
            if (newProgram.IsActive == true)
            {
                LiteralIsActive.Text = "Yes";
            }
            else
            {
                LiteralIsActive.Text = "No";
            }
        }

        /// <summary>
        /// Update Program
        /// </summary>
        private void AmendProgram()
        {
            SD.DataAccess.Program newProgram = new SD.DataAccess.Program();
            newProgram.ProgramId = Convert.ToInt32(Request.QueryString["programId"].ToString());
            newProgram.ProgramName = TextBoxName.Text;
            newProgram.Description = TextBoxDescription.Text;
            if (!string.IsNullOrEmpty(HiddenFieldManagerId.Value))
            {
                newProgram.ContactId = Convert.ToInt16(HiddenFieldManagerId.Value);
            }
            if (LiteralIsActive.Text == "Yes")
            {
                newProgram.IsActive = true;
            }
            else
            {
                newProgram.IsActive = false;
            }
            bool success = new ProgramBUS().AmendProgram(newProgram);
            //check update
            if (success == true)
            {
                lblCheck.Visible = false;
                lblCheckSuccess.Text = "Edit successfully!!";
                lblCheckSuccess.Visible = true;
            }
        }

        /// <summary>
        /// Button Save Click event
        /// </summary>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            if (TextBoxName.Text.Trim() == "")
            {
                lblCheck.Text = "Invalid name!";
                lblCheck.Visible = true;
                lblCheckSuccess.Visible = false;
                return;
            }
            ProgramView newProgram = new ProgramBUS().GetProgram(Convert.ToInt32(Request.QueryString["programId"].ToString()));
            //check new name != old name ?
            if (newProgram.ProgramName != TextBoxName.Text)
            {
                //check name have already existed ?
                bool checkName = new ProgramBUS().CheckNameProgram(TextBoxName.Text);
                if (checkName == false)
                {
                    AmendProgram();
                }
                else
                {
                    lblCheck.Text = "Program name has already existed!";
                    lblCheck.Visible = true;
                    lblCheckSuccess.Visible = false;
                }
            }
            else
            {
                AmendProgram();
            }
        }

        /// <summary>
        /// Button Deactivate Click event
        /// </summary>
        protected void ButtonDeactivate_Click(object sender, EventArgs e)
        {
            if (LiteralIsActive.Text == "Yes")
            {
                //update status = true
                bool success = new ProgramBUS().ChangeProgramStatus(Convert.ToInt32(Request.QueryString["programId"].ToString()), false);
                if (success == true)
                {
                    LoadData();
                    lblCheck.Visible = false;
                    lblCheckSuccess.Text = "Edit status successfully!";
                    lblCheckSuccess.Visible = true;
                }
            }
            else
            {
                lblCheck.Visible = false;
                lblCheckSuccess.Text = "Program is deactived!";
                lblCheckSuccess.Visible = true;
            }
        }

        /// <summary>
        /// Button Back Click event
        /// </summary>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProgramList.aspx?Page=1");
        }

        /// <summary>
        /// Button Lookup Click event
        /// </summary>
        protected void LinkButtonLookup_Click(object sender, EventArgs e)
        {
            this.ContactLookup1.Popup();
        }
    }
}